<?php
include("include/conn.php");
require('include/Smarty/libs/Smarty.class.php');

$garnuDate        = "";
$garnuarray       = array();
$garnu            = array();
$garnuDetail      = array();
$garnuOtherArray  = array();
$garnuText2Tot    = 0;
$garnuMasterId    = isset($_GET['garnuMasterId']) ? $_GET['garnuMasterId'] : 0;
$garnuNameMaster  = "";
$garnuDateMaster  = "";
$weightMaster     = "";
$touchMaster      = "";
$silverMaster     = "";
$copperMaster     = "";
$retWeightMaster  = "";
$isEdit = "";
if(isset($_POST['submitBtn']) && isset($_POST['garnuName']) )
{
  if($_POST['garnuMasterId'] > 0)
  {
    $updateGarnuQuery     = "UPDATE garnumaster SET garnuName = '".$_POST['garnuName']."', garnuDate = '".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."',
                                    weight = ".$_POST['totalWeight'].",returnWeight = ".$_POST['returnWeight'].",touch = ".$_POST['totalWeightTouch'].",silver = ".$_POST['totalSilver'].",copper = ".$_POST['totalCopper']."
                              WHERE garnuMasterId = ".$_POST['garnuMasterId']."";
    $updateGarnuQueryRes  =  mysql_query($updateGarnuQuery);
    $$garnuMasterId       =  $_POST['garnuMasterId'];
    //Before Update We Delete Row :Start
    $deleteGarnuDetail    = "DELETE FROM garnudetail 
                              WHERE garnuMasterId = ".$garnuMasterId;
    $deleteGarnuDetailRes = mysql_query($deleteGarnuDetail);
    //Before Update We Delete Row :End
	  $loopCount1 = 0;
    while($loopCount1 < count($_POST['weightEntered']))
    {
      $weight = ($_POST['weightEntered'][$loopCount1] != "" ) ? $_POST['weightEntered'][$loopCount1]: 0;
      $touch =  ($_POST['touchEntered'][$loopCount1]  != "" ) ? $_POST['touchEntered'][$loopCount1] : 0;
      $silver = ($_POST['silverEntered'][$loopCount1] != "" ) ? $_POST['silverEntered'][$loopCount1]: 0;
      $copper = ($_POST['copperEntered'][$loopCount1] != "" ) ? $_POST['copperEntered'][$loopCount1]: 0;
      if($_POST['weightEntered'][$loopCount1] != "" || $_POST['touchEntered'][$loopCount1] != "" || $_POST['silverEntered'][$loopCount1] != "" || $_POST['copperEntered'][$loopCount1] != "" )
      {
        $insertDetailQuery = "INSERT INTO garnudetail (garnuMasterId,weight,touch,silver,copper)
                              VALUES (".$garnuMasterId.",".$weight.",".$touch.",".$silver.",".$copper.")";
        $insertDetailQueryRes = mysql_Query($insertDetailQuery);
      }
      $loopCount1++;
    }
    //Before Update We Delete Row :Start
    $deleteGarnuDetail    = "DELETE FROM garnuother 
                              WHERE garnuMasterId = ".$garnuMasterId;
    $deleteGarnuDetailRes = mysql_query($deleteGarnuDetail);
    //Before Update We Delete Row :End    
    $loopCountText = 0;
    while($loopCountText < count($_POST['text1']))
    {
      $text1 = ($_POST['text1'][$loopCountText] != "") ? $_POST['text1'][$loopCountText] : 0;
      $text2 = ($_POST['text2'][$loopCountText] != "") ? $_POST['text2'][$loopCountText] : 0;
      if($_POST['text1'][$loopCountText] != "" || $_POST['text2'][$loopCountText] != "" )
      {
        $insertQueryDetail = "INSERT INTO garnuother (garnuMasterId,text1,text2)
                              VALUES (".$garnuMasterId.",'".$text1."',".$text2.")";
  	    $insertQueryDetailResult = mysql_query($insertQueryDetail);
      }
      $loopCountText++;
    }  
    header("Location:garnuList.php"); 
  }
  else
  {
    $returnWeight       = $_POST['returnWeight'] == "" ? 0 : $_POST['returnWeight'];
    
	  $insertMasterQuery    = "INSERT INTO garnumaster (garnuName,garnuDate,weight,returnWeight,touch,silver,copper)
	                           VALUES ('".$_POST['garnuName']."','".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."',
	                           ".$_POST['totalWeight'].",".$returnWeight.",".$_POST['totalWeightTouch'].",".$_POST['totalSilver'].",".$_POST['totalCopper'].")";
	  $insertmasterQueryRes = mysql_Query($insertMasterQuery);
	  $garnuMasterId = mysql_insert_id();
	
	  $loopCount1 = 0;
    while($loopCount1 < count($_POST['weightEntered']))
    {
      $weight = ($_POST['weightEntered'][$loopCount1] != "" ) ? $_POST['weightEntered'][$loopCount1]: 0;
      $touch =  ($_POST['touchEntered'][$loopCount1]  != "" ) ? $_POST['touchEntered'][$loopCount1] : 0;
      $silver = ($_POST['silverEntered'][$loopCount1] != "" ) ? $_POST['silverEntered'][$loopCount1]: 0;
      $copper = ($_POST['copperEntered'][$loopCount1] != "" ) ? $_POST['copperEntered'][$loopCount1]: 0;
      if($_POST['weightEntered'][$loopCount1] != "" || $_POST['touchEntered'][$loopCount1] != "" || $_POST['silverEntered'][$loopCount1] != "" || $_POST['copperEntered'][$loopCount1] != "" )
      {
        $insertDetailQuery = "INSERT INTO garnudetail (garnuMasterId,weight,touch,silver,copper)
                              VALUES (".$garnuMasterId.",".$weight.",".$touch.",".$silver.",".$copper.")";
        $insertDetailQueryRes = mysql_Query($insertDetailQuery);
      }
      $loopCount1++;
    }
  }
}           

if(isset($_GET['garnuMasterId']) && $_GET['garnuMasterId'] > 0 )
{
  $selectGarnuMaster     = "SELECT garnuMasterId,garnuName,garnuDate,weight,returnWeight,touch,silver,copper FROM garnumaster
                             WHERE garnuMasterId = ".$_GET['garnuMasterId'];
  $selectGarnuMasterRes  = mysql_query($selectGarnuMaster);
  $garnuMasterrow        = mysql_fetch_assoc($selectGarnuMasterRes);
  $garnuDateMaster       = $garnuMasterrow['garnuDate'];
  $garnuNameMaster       = $garnuMasterrow['garnuName'];
  $weightMaster          = $garnuMasterrow['weight'];
  $touchMaster           = $garnuMasterrow['touch'];
  $retWeightMaster       = $garnuMasterrow['returnWeight'];
  $silverMaster          = $garnuMasterrow['silver'];
  $copperMaster          = $garnuMasterrow['copper'];
  
  $selectBhatthi = "SELECT garnudetail.garnuMasterId,garnudetail.weight,garnudetail.touch,garnudetail.silver,
                           garnudetail.copper,garnumaster.garnuMasterId 
                      FROM garnudetail
                      JOIN garnumaster
                     WHERE garnudetail.garnuMasterId = garnumaster.garnuMasterId
                       AND garnumaster.garnuMasterId = ".$_GET['garnuMasterId'];
  $selectBhatthiRes = mysql_query($selectBhatthi);
  $b = 0;
  while($garnuRow = mysql_fetch_assoc($selectBhatthiRes))
  {
    $garnuarray[$b]['weightEntered']      = $garnuRow['weight'];
    $garnuarray[$b]['touchEntered']       = $garnuRow['touch'];
    $garnuarray[$b]['silverEntered']      = $garnuRow['silver'];
    $garnuarray[$b]['copperEntered']      = $garnuRow['copper'];
    $b++;
  }
  
  $selectGarnuQuery = "SELECT garnuother.garnuOtherId,garnuother.garnuMasterId,garnuother.text1,garnuother.text2,garnumaster.garnuName
                         FROM garnuother
                         JOIN garnumaster
                        WHERE garnuother.garnuMasterId = garnumaster.garnuMasterId
                          AND garnuother.garnuMasterId = ".$_GET['garnuMasterId']."
                          ORDER BY garnuOtherId";
  $selectGarnuQueryResult = mysql_query($selectGarnuQuery);
  $l = 0;
  while($garnuOtherRow = mysql_fetch_array($selectGarnuQueryResult))
  {
    $garnuOtherArray[$l]['garnuOtherId']  = $garnuOtherRow['garnuOtherId'];
    $garnuOtherArray[$l]['garnuMasterId'] = $garnuOtherRow['garnuMasterId'];
    $garnuOtherArray[$l]['text1']         = $garnuOtherRow['text1'];
    $garnuOtherArray[$l]['text2']         = $garnuOtherRow['text2'];
    $garnuText2Tot                            += $garnuOtherRow['text2'];
    $l++;
  } 
}
        
$selectParty = "SELECT garnuMasterId,garnuDate
                  FROM garnumaster";
$selectPartyRes = mysql_query($selectParty);
$a = 0;
while($row = mysql_fetch_assoc($selectPartyRes))
{
  $garnu['garnuDate'][$a]     = $row['garnuDate'];
  $garnu['garnuMasterId'][$a] = $row['garnuMasterId'];
  $a++;
}

$smarty = new Smarty;
require('bottom.php');
$smarty->assign("garnuOtherArray",$garnuOtherArray);
$smarty->assign("garnuText2Tot",$garnuText2Tot);
$smarty->assign("garnuDate",$garnuDate);
$smarty->assign("garnuarray",$garnuarray);
$smarty->assign("garnu",$garnu);
$smarty->assign("garnuMasterId",$garnuMasterId);
$smarty->assign("garnuNameMaster",$garnuNameMaster);
$smarty->assign("garnuDateMaster",$garnuDateMaster);
$smarty->assign("weightMaster",$weightMaster);
$smarty->assign("touchMaster",$touchMaster);
$smarty->assign("silverMaster",$silverMaster);
$smarty->assign("copperMaster",$copperMaster);
$smarty->assign("retWeightMaster",$retWeightMaster);
$smarty->assign("garnuDetail",$garnuDetail);
$smarty->display('garnu.tpl');
?>